using System;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Collections.Generic;
using System.Data.Common;
using VSSAHT.Transfer;
using VSSAHT.DataAccess;
using VSS_AHT.DataAccess;

namespace VSSAHT.Database
{
	public class ModelVehicleDataAccessBase : BaseDataAccess, IModelVehicleDataAccessBase
	{
		public IList<ModelVehicleDTO> GetAll()
		{
			IList<ModelVehicleDTO> list = null;
			// note: here you can use any persistance library
			// to dynamically populate a list of all objects
			try
			{
				// Process
				using (IDbConnection connection = DbProviderFactory.CreateConnection())
				{
					connection.ConnectionString = ConnectionString;
                    using (VSSAHTDataContext biocode = new VSSAHTDataContext(connection))
					{
						list = biocode.ModelVehicles.Select(e => ModelVehicleMapper.ToBusinessObject(e)).ToList();
					}
				}
			}
			catch
			{
			}
			return list;
		}
		public bool Add(ModelVehicleDTO dto)
		{
			bool isSuccess = false;
			// note: here you can use any persistance library
			// to dynamically populate a list of all objects
			try
			{
				// Process
				using (IDbConnection connection = DbProviderFactory.CreateConnection())
				{
					connection.ConnectionString = ConnectionString;
					using(VSSAHTDataContext biocode = new VSSAHTDataContext(connection))
					{
						if (OnBeforeAdd(biocode, dto))
						{
							var entity = ModelVehicleMapper.ToEntity(new ModelVehicle(), dto);
							try
							{
								biocode.ModelVehicles.InsertOnSubmit(entity);
								biocode.SubmitChanges();
								isSuccess = true;
							}
							catch(ChangeConflictException)
							{
								//  A possible concurrency exception occurred.  Let's see if
								//  we can resolve it.
								foreach(var conflict in biocode.ChangeConflicts)
								{
									conflict.Resolve(RefreshMode.KeepCurrentValues);
								}
								try
								{
									biocode.SubmitChanges();
									isSuccess = true;
								}
								catch(ChangeConflictException)
								{
									//  It didn't work, so throw a new exception.
									throw new Exception("A concurrency error occurred!");
								}
							}
							catch(Exception)
							{
								throw new Exception("There was an error saving this record!");
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				_errorMessage = ex.Message;
			}
			return isSuccess;
		}
		public bool Edit(ModelVehicleDTO dto)
		{
			bool isSuccess = false;
			// note: here you can use any persistance library
			// to dynamically populate a list of all objects
			try
			{
				// Process
				using (IDbConnection connection = DbProviderFactory.CreateConnection())
				{
					connection.ConnectionString = ConnectionString;
					using(VSSAHTDataContext biocode = new VSSAHTDataContext(connection))
					{
						if (OnBeforeEdit(biocode, dto))
						{
							var entity = ModelVehicleMapper.ToEntity(new ModelVehicle(), dto);
							try
							{
								biocode.ModelVehicles.Attach(entity, true);
								biocode.SubmitChanges();
								isSuccess = true;
							}
							catch(ChangeConflictException)
							{
								//  A possible concurrency exception occurred.  Let's see if
								//  we can resolve it.
								foreach(var conflict in biocode.ChangeConflicts)
								{
									conflict.Resolve(RefreshMode.KeepCurrentValues);
								}
								try
								{
									biocode.SubmitChanges();
									isSuccess = true;
								}
								catch(ChangeConflictException)
								{
									//  It didn't work, so throw a new exception.
									throw new Exception("A concurrency error occurred!");
								}
							}
							catch(Exception)
							{
								throw new Exception("There was an error saving this record!");
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				_errorMessage = ex.Message;
			}
			return isSuccess;
		}
		public bool Delete(ModelVehicleDTO dto)
		{
			bool isSuccess = false;
			// note: here you can use any persistance library
			// to dynamically populate a list of all objects
			try
			{
				// Process
				using (IDbConnection connection = DbProviderFactory.CreateConnection())
				{
					connection.ConnectionString = ConnectionString;
					using(VSSAHTDataContext biocode = new VSSAHTDataContext(connection))
					{
						if (OnBeforeDelete(biocode, dto))
						{
							var entity = ModelVehicleMapper.ToEntity(new ModelVehicle(), dto);
							try
							{
								biocode.ModelVehicles.Attach(entity, false);
								biocode.ModelVehicles.DeleteOnSubmit(entity);
								biocode.SubmitChanges();
								isSuccess = true;
							}
							catch(ChangeConflictException)
							{
								throw new Exception("A concurrency error occurred!");
							}
							catch(Exception)
							{
								throw new Exception("There was an error saving this record!");
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				_errorMessage = ex.Message;
			}
			return isSuccess;
		}
		#region Extensibility Method Declarations

		protected virtual bool OnBeforeAdd(VSSAHTDataContext biocode, ModelVehicleDTO dto) { return true; }
		protected virtual bool OnBeforeEdit(VSSAHTDataContext biocode, ModelVehicleDTO dto) { return true; }
		protected virtual bool OnBeforeDelete(VSSAHTDataContext biocode, ModelVehicleDTO dto) { return true; }

		#endregion

	}
}
